package com.tinshow.config.mybatis;

import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import javax.sql.DataSource;
import java.util.Properties;


@Configuration
//@AutoConfigureAfter({ DataBaseConfiguration.class })
@MapperScan(basePackages="com.tinshow.dao.**")
@Slf4j
public class MyBatisConfiguration implements TransactionManagementConfigurer{
	

//	private List<DataSourceDetail> slaveList;
//    @Bean
//    public SqlSessionFactory sqlSessionFactorys() throws Exception {
//        logger.info("-------------------- 重载父类 sqlSessionFactory init ---------------------");
//        return super.sqlSessionFactory(roundRobinDataSouceProxy());
//    }
//
//    @Bean
//    public AbstractRoutingDataSource roundRobinDataSouceProxy(){
//    	slaveList = PoolThread.getMasterMap().get("slave");
//    	int size = slaveList.size();
//		MyAbstractRoutingDataSource proxy = new MyAbstractRoutingDataSource(size);
//		Map<Object, Object> targetDataSources = new HashMap<Object, Object>();
//		DataSource writeDataSource = SpringContextHolder.getBean("writeDataSource");
//		// 写
//		targetDataSources.put(DataSourceType.write.getType(), SpringContextHolder.getBean("writeDataSource"));
//
//		for (int i = 0; i < size; i++) {
//			targetDataSources.put(i, SpringContextHolder.getBean("readDataSource" + (i + 1)));
//		}
//		proxy.setDefaultTargetDataSource(writeDataSource);
//		proxy.setTargetDataSources(targetDataSources);
//		return proxy;
//    }
      
//    @Bean  
//    @ConditionalOnMissingBean  
//    public DataSourceTransactionManager transactionManager() {  
//        return new DataSourceTransactionManager(writeDataSource);  
//    }

	@Autowired
	private DataSource dataSource;

	@Bean(name = "sqlSessionFactory")
	public SqlSessionFactory sqlSessionFactoryBean() {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		// 根据实际配置“实体类”路径 *
		bean.setTypeAliasesPackage("com.tinshow.dao");
		// 分页插件
		PageHelper pageHelper = new PageHelper();
		Properties properties = new Properties();
		properties.setProperty("reasonable", "true");
		properties.setProperty("supportMethodsArguments", "true");
		properties.setProperty("returnPageInfo", "check");
		properties.setProperty("params", "count=countSql");
		pageHelper.setProperties(properties);
		//添加插件
		bean.setPlugins(new org.apache.ibatis.plugin.Interceptor[]{pageHelper});
		//添加XML目录
		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		try {
			// 根据实际配置“mapper.xml”路径
			bean.setMapperLocations(resolver.getResources("/mapper/*/*.xml"));
			return bean.getObject();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
		@Bean
		public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
			return new SqlSessionTemplate(sqlSessionFactory);
	}
	@Bean
	@Override
	public PlatformTransactionManager annotationDrivenTransactionManager() {
		return new DataSourceTransactionManager(dataSource);
	}


}
